package com.realtyManage.springboot.Service.impl;

import com.realtyManage.springboot.Dao.ActiveRepository;
import com.realtyManage.springboot.Model.Do.Active;
import com.realtyManage.springboot.Model.Vo.RewardDetailEntity;
import com.realtyManage.springboot.Service.IActiveService;
import com.realtyManage.springboot.Service.IArticleService;
import com.realtyManage.springboot.Service.ICustomerService;
import com.realtyManage.springboot.Service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.Predicate;
import javax.transaction.Transactional;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * ActiveServiceImpl
 *
 * @Author 杭州素色网络科技有限公司
 * @Date 2019/5/17
 **/
@Service
@Transactional
public class ActiveServiceImpl implements IActiveService {
    @Autowired
    private ActiveRepository activeRepository;
    @Autowired
    private IArticleService articleService;
    @Autowired
    private IUserService userService;
    @Autowired
    private ICustomerService customerService;

    @Override
    public int takePartIn(Integer uid, int artId) {
        boolean pass = false;
        // Article article = articleService.getOne(artId);
        int status = 0;//默认参与活动状态为未结算
        /*if(article.getType()==3){//超级奖励，判断参与条件是否满足
            User user = userService.findById(uid);
            int crossLine = 0;
            int realLine = 0;
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            switch (article.getCondition1()){
                case 1://1.新注册普通会员
                    if(user.getJoinTime().after(article.getStartTime())&&user.getJoinTime().before(article.getEndTime())){
                        pass = true;
                        status = 1;
                    }
                    break;
                case 2://2.新注册独立经纪人
                    if(user.getJoinTime().after(article.getStartTime())&&user.getJoinTime().before(article.getEndTime())){
                        pass = true;
                        status = 1;
                    }
                    break;
                case 3://3.新注册门店经纪人
                    if(user.getJoinTime().after(article.getStartTime())&&user.getJoinTime().before(article.getEndTime())){
                        pass = true;
                        status = 1;
                    }
                    break;
                case 4://4.新注册门店负责人
                    if(user.getJoinTime().after(article.getStartTime())&&user.getJoinTime().before(article.getEndTime())){
                        pass = true;
                        status = 1;
                    }
                    break;
                case 5://5.发展下线xx名
                    crossLine = article.getCondition2();
                    realLine = userService.countByUpIdAndJoinTimeBetween(user.getId(),article.getStartTime(),article.getEndTime());
                    if(realLine>= crossLine) {
                        pass = true;
                        status = 1;
                    }
                    break;
                case 6://6.报备xx组客户
                    crossLine = article.getCondition2();
                    realLine = customerService.countByConnUserIdAndStatusAndLastOperateDateBetween(user.getId(),1,article.getStartTime(),article.getEndTime());
                    if(realLine>=crossLine) {
                        pass = true;
                        status = 1;
                    }
                    break;
                case 7://7.带访xx组客户
                    crossLine = article.getCondition2();
                    realLine = customerService.countByConnUserIdAndStatusAndLastOperateDateBetween(user.getId(),2,article.getStartTime(),article.getEndTime());
                    if(realLine>=crossLine) {
                        pass = true;
                        status = 1;
                    }
                    break;
                case 8://8.成交xx组客户
                    crossLine = article.getCondition2();
                    realLine = customerService.countByConnUserIdAndStatusAndLastOperateDateBetween(user.getId(),4,article.getStartTime(),article.getEndTime());
                    if(realLine>=crossLine) {
                        pass = true;
                        status = 1;
                    }
                    break;
            }
        }*/

        /*if(pass == false){
            return -1032;
        }*/
        List<Active> activeList = activeRepository.findByUidAndArtId(uid,artId);
        if(activeList.size()>0){
            return -1031;
        }
        Active active = new Active();
        active.setUid(uid);
        active.setArtId(artId);
        active.setTime(new Date());
        active.setStatus(status);
        activeRepository.save(active);
        return 1;
    }

    @Override
    public List<Active> censusActiveInfo(Integer activeId) {
        return activeRepository.findByArtId(activeId);
    }

    @Override
    public int countByArtId(Integer artId) {
        return activeRepository.countByArtId(artId);
    }

    @Override
    public Page<Active> findPagedByWhereClause(RewardDetailEntity rewardDetailEntity) {
        Integer pageNumber = rewardDetailEntity.getPageNumber()-1;
        Integer pageSize = rewardDetailEntity.getPageSize();

        //排序条件
        Sort sort = new Sort(Sort.Direction.DESC, "id");
        if(rewardDetailEntity.getSort() != null){
            if (rewardDetailEntity.getSort().equals("DESC")) {
                sort = new Sort(Sort.Direction.DESC, rewardDetailEntity.getSortField());
            } else if(rewardDetailEntity.getSort().equals("ASC")){
                sort = new Sort(Sort.Direction.ASC, rewardDetailEntity.getSortField());
            }
        }
        Pageable pageable = PageRequest.of(pageNumber,pageSize,sort);

        //动态查询条件
        Specification<Active> specification = (Specification<Active>)(root, query, criteriaBuilder) -> {
            List<Predicate> list = new ArrayList<>();
            if (rewardDetailEntity.getUid()!=null) {
                // 根据参与用户id查询
                Predicate p1 = criteriaBuilder.equal(root.get("uid"),rewardDetailEntity.getUid());
                list.add(p1);
            }
            if (rewardDetailEntity.getArtId()!=null) {
                // 根据活动id查询
                Predicate p2 = criteriaBuilder.equal(root.get("artId"),rewardDetailEntity.getArtId());
                list.add(p2);
            }

            return criteriaBuilder.and(list.toArray(new Predicate[0]));
        };
        return activeRepository.findAll(specification, pageable);
    }
}
